# shopify_dev/app/config.py
import os
import logging

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger('shopify_app')


class Config:
    DEBUG = False
    JSON_AS_ASCII = False
    
    # 安全配置
    SECRET_KEY = os.getenv("SECRET_KEY", "your_secure_secret_key_here_change_in_production")
    
    # 代理配置(按需启用)
    HTTP_PROXY = os.getenv("HTTP_PROXY", "http://127.0.0.1:7897")
    HTTPS_PROXY = os.getenv("HTTPS_PROXY", "http://127.0.0.1:7897")

    # 数据库连接配置
    DB_HOST = os.getenv("DB_HOST", "localhost")
    DB_PORT = int(os.getenv("DB_PORT", 3306))
    DB_USER = os.getenv("DB_USER", "root")
    DB_PASSWORD = os.getenv("DB_PASSWORD", "123456")
    DB_NAME = os.getenv("DB_NAME", "shopify_app")
    DB_CHARSET = "utf8mb4"

    # 连接池配置
    DB_POOL_SIZE = 5
    DB_MAX_OVERFLOW = 10
    DB_POOL_RECYCLE = 3600
    
    # Shopify App 配置
    SHOPIFY_API_KEY = os.getenv("SHOPIFY_API_KEY", "your_shopify_api_key")
    SHOPIFY_API_SECRET = os.getenv("SHOPIFY_API_SECRET", "your_shopify_api_secret")
    SHOPIFY_TOKEN_URL = "https://{shop_domain}/admin/oauth/access_token"
    SHOPIFY_API_SCOPES = [
        "read_orders",
        "write_orders",
        "read_products",
        "read_customers",
        "write_script_tags",
        "read_script_tags",
        "read_price_rules",
        "write_price_rules",
        "read_discounts",
        "write_discounts"
    ]
    
    # Webhook 配置
    SHOPIFY_WEBHOOK_TOPICS = [
        "orders/create",
        "orders/paid",
        "refunds/create",
        "orders/updated",
        "shop/redact"
    ]
    
    # Shopify 认证回调 URL
    SHOPIFY_REDIRECT_URI = os.getenv("SHOPIFY_REDIRECT_URI", "http://localhost:5000/auth/callback")
    
    # 积分兑换比例
    POINTS_PER_DOLLAR = 1
    
    # 缓存和安全配置
    SESSION_COOKIE_SECURE = False  # 生产环境应设为 True
    OAUTH_STATE_EXPIRY = 300  # OAuth state 过期时间（秒）
    RATE_LIMIT_PER_MINUTE = 60  # 每分钟请求限制


# 实例化配置
config = Config()